package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.mapper.DepartmentMapper;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DepartmentServiceImpl implements IDepartmentService {

	@Autowired
	private DepartmentMapper departmentMapper;

	public void delete(Long id) {
		departmentMapper.delete(id);
	}

	public void insert(Department department) {
		if (StringUtil.hasLength(department.getName()) || StringUtil.hasLength(department.getSn())) {
			departmentMapper.insert(department);
		}else {
			throw new RuntimeException("请确保输入完整");
		}
	}

	public Department get(Long id) {
		return departmentMapper.get(id);
	}

	public List<Department> selectAll() {
		return departmentMapper.selectAll();
	}

	public void update(Department department) {
		if (StringUtil.hasLength(department.getName()) || StringUtil.hasLength(department.getSn())) {
			departmentMapper.update(department);
		}else {
			throw new RuntimeException("请确保输入完整");
		}
	}

	public PageInfo<Department> query(QueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		return new PageInfo<Department>(departmentMapper.selectForList(qo));
	}
}
